package com.mq.batch.consumer2;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ytj
 * @date 2024/1/15
 */
@Configuration
public class Demo6Config {

    @Bean
    public Queue demo6Queue() {
        return new Queue(Demo6Message.QUEUE_NAME, true,false,false);
    }

    @Bean
    public Exchange demo6Exchange() {
        return new DirectExchange(Demo6Message.EXCHANGE_NAME,true,false);
    }

    @Bean
    public Binding demo6Binding() {
        return BindingBuilder.bind(demo6Queue()).to(demo6Exchange()).with(Demo6Message.ROUTE_KEY).noargs();
    }

    @Bean(name = "consumerBatchContainerFactory")
    public SimpleRabbitListenerContainerFactory consumerBatchContainerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory){
        //创建simpleRabbitListenerContainerFactory对象
        SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory=new SimpleRabbitListenerContainerFactory();
        configurer.configure(simpleRabbitListenerContainerFactory,connectionFactory);
        //添加批量消费属性
        simpleRabbitListenerContainerFactory.setBatchListener(true);
        //最大消息数
        simpleRabbitListenerContainerFactory.setBatchSize(10);
        //最大等待时间
//        simpleRabbitListenerContainerFactory.setReceiveTimeout(30000L);
        simpleRabbitListenerContainerFactory.setConsumerBatchEnabled(true);
        return simpleRabbitListenerContainerFactory;
    }
}
